﻿//////////////////////////////////////////////////////////////////////////////////
//
// Project            : XNAsteroids
// Module             : XNAsteroidsGameObjects
// Description        : Collection of projectiles.
// 
// Repository         : $URL$
// Last changed by    : $LastChangedBy$
// Revision           : $LastChangedRevision$
// Last Changed       : $LastChangedDate$
// Author             : $Author$
//
// Id                 : $Id$
//
// Copyright          : (c) 2010 Torsten Bär
//
// Published under the MIT License. See license.txt or http://www.opensource.org/licenses/mit-license.php.
//
//////////////////////////////////////////////////////////////////////////////////

using System;
using System.Linq;
using Microsoft.Xna.Framework;

namespace XNAsteroids.GameObjects.SpaceObjects
{
   /// <summary>
   /// Collection of projectiles.
   /// </summary>
   public class ProjectileCollection : SpaceObjectCollection<Projectile>
   {
      #region Constructor

      /// <summary>
      /// Initializes a new instance of the <see cref="ProjectileCollection"/> class.
      /// </summary>
      /// <param name="game">The Game that the game component should be attached to.</param>
      public ProjectileCollection(Game game)
         : base(game)
      {}

      #endregion

      #region Methods

      /// <summary>
      /// Called when the GameComponent needs to be updated. Override this method with component-specific update code.
      /// </summary>
      /// <param name="gameTime">Time elapsed since the last call to Update</param>
      public override void Update(GameTime gameTime)
      {
         base.Update(gameTime);

         innerList
            .Where(projectile => ((Projectile)projectile).RemainingLifetime <= 0)
            .ToList()
            .ForEach(item =>
                     {
                        innerList.Remove(item);
                        ((Projectile)item).Dispose();
                     });
      }

      /// <summary>
      /// Called when the DrawableGameComponent needs to be drawn. Override this method with component-specific drawing code. Reference page contains links to related conceptual articles.
      /// </summary>
      /// <param name="gameTime">Time passed since the last call to Draw.</param>
      public override void Draw(GameTime gameTime)
      {
         innerList.Cast<DrawableGameComponent>().ForEach(item => item.Draw(gameTime));
      }

      #endregion
   }
}